<?php
require_once '../php/database.php';

if(isset($_POST)){
	$db = new DatabaseManager();
	$connection = $db->getDB();
	$query = "select id,req_id,version_id,time_stamp,new_status+0 as new_status from change_in_status where req_id=".$_POST['id'];
	$result = mysqli_query($connection,$query);
	
	$data = array("id"=>"","req_id"=>"","version_id"=>"","time_stamp"=>"","new_status"=>"");
	$darray = array($data);
	if($result){
		if(mysqli_num_rows($result)>0){
			$i=0;
			while($temp = mysqli_fetch_array($result,MYSQLI_ASSOC)){
				$data['id'] = $temp['id'];
				$data['req_id'] = $temp['req_id'];
				$data['version_id'] = $temp['version_id'];
				$data['time_stamp'] = $temp['time_stamp'];
				switch($temp['new_status']){
					case 1:$data['new_status']="Request was made/Pending at advisor";break;
					case 2:$data['new_status']="Advisor forwards to pgc";break;
					case 3:$data['new_status']="Approved by advisor/pgc, now waiting at office";break;
					case 4:$data['new_status']="Sent back for modification";break;
					case 5:$data['new_status']="Rejected";break;
					case 6:$data['new_status']="Approved by office, and balanace has been deducted";break;
				}
				$darray[$i]=$data;
				$i++;
			}
		}
	}
	mysqli_free_result($result);
	if($i==0)
		echo "Soemthign went wrong!";
	else {
		$j=0;
		foreach($darray as $row){
			$j++;
			$db = new DatabaseManager();
			$connection = $db->getDB();
			$query = "call whois('".$row['id']."')";
			$result = mysqli_query($connection,$query);
			if($result){
				if(mysqli_num_rows($result)>0){
					$temp = mysqli_fetch_array($result,MYSQLI_ASSOC);
					$darray[0]['id']=$temp['name'];	
				}
			}
		}
		foreach($darray as $row){
		echo $row['id']." ".$row['req_id']."  ".$row['version_id']."  ".$row['time_stamp']." ".$row['new_status'];
	}
	}
	
}


?>
